package com.atguigu.blog.service.impl;

import com.atguigu.blog.exception.PasswordErrorException;
import com.atguigu.blog.exception.UsernameErrorException;
import com.atguigu.blog.mapper.UserMapper;
import com.atguigu.blog.pojos.User;
import com.atguigu.blog.result.ResultCodeEnum;
import com.atguigu.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author CF
 * @date 2023/8/19 10:24
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User doLogin(User user) {
        //1.根据用户名查询用户
        User dbuser = userMapper.findByUsername(user.getUsername());
        //2.判断用户是否存在
        if (dbuser == null) {
            throw new UsernameErrorException();
        }
        //3.如果用户名正确，则判断密码是否正确
        if (!dbuser.getPassword().equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException();
        }
            //登录成功   将密码置空
        dbuser.setPassword(null);
        return dbuser;
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateUser(user);
    }
}
